package com.example.guiguiapplicatio.api;

import android.content.Context;
import android.os.NetworkOnMainThreadException;
import android.util.Log;

import com.example.guiguiapplicatio.api.Interface.InsertRecordCallback;
import com.example.guiguiapplicatio.util.ResponseBody;
import com.example.guiguiapplicatio.utils.PreferencesUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import static com.example.guiguiapplicatio.api.GetCountTotalApi.ip;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class InsertRecordApi {
    private final Gson gson = new Gson();
    private InsertRecordCallback insertRecordCallback;
    public void setInsertRecordCallback(InsertRecordCallback insertRecordCallback){
        this.insertRecordCallback=insertRecordCallback;
    }
    public void performInsertRecord(String recordName, String idcard, String nation, String residence, String detailedresidence, String birthday, String phone, String sex, List<Integer>peopleidlist, Context content){
        new Thread(()->{
            String url = "http://"+ip +":9090/recordadd";
            Headers headers = new Headers.Builder()
                    .add("Accept", "application/json, text/plain, */*")
//                    .add("appId", "用户所申请的应用ID")
//                    .add("appSecret", "用户所申请的应用密钥")
                    .add("Content-Type", "application/json")
                    .build();
            Map<String, Object> bodyMap = new HashMap<>();
            bodyMap.put("idcard", idcard);
            bodyMap.put("name", recordName);
            bodyMap.put("nation", nation);
            bodyMap.put("residence", residence);
            bodyMap.put("detailedresidence", detailedresidence);
            bodyMap.put("doctorid", PreferencesUtil.getInt(content,"doctor_id"));
            bodyMap.put("time", birthday);
            bodyMap.put("phone", phone);
            bodyMap.put("sex", sex);
            bodyMap.put("peopleidlist", peopleidlist);

            String body=gson.toJson(bodyMap);
            MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
            Request request = new Request.Builder()
                    .url(url)
                    // 将请求头加至请求中
                    .headers(headers)
                    .post(RequestBody.create(MEDIA_TYPE_JSON, body))
                    .build();
            try {
                OkHttpClient client = new OkHttpClient();
                //发起请求，传入callback进行回调
                client.newCall(request).enqueue(callback);
            }catch (NetworkOnMainThreadException ex){
                ex.printStackTrace();
            }
        }).start();

    }
    private final Callback callback=new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();
            if (insertRecordCallback != null) {
                insertRecordCallback.insertFailure(e.getMessage());
            }
        }
        @Override
        public void onResponse(Call call, Response response) throws IOException {
            Type jsonType = new TypeToken<ResponseBody<Object>>(){}.getType();
            // 获取响应体的json串
            String body = response.body().string();
            Log.d("info", body);
            // 解析json串到自己封装的状态
            ResponseBody<Object> dataResponseBody = gson.fromJson(body,jsonType);
            Log.d("info", dataResponseBody.toString());
            if (insertRecordCallback != null) {
                insertRecordCallback.insertSuccess(dataResponseBody);
            }
        }
    };
}
